#!/usr/bin/python
#-*- coding: utf-8 -*-
import gconf
import os

class GConfigurator(object):
	def __init__(self, rootFolder):
		self.types = {
			'bool': (self.getBooleanPrivate, self.setBoolean), 
			'string': (self.getStringPrivate, self.setString),
			'int': (self.getIntegerPrivate, self.setInteger) }
			
		self.rootFolder = rootFolder
		self.client = gconf.client_get_default()
		self.client.add_dir(rootFolder, gconf.CLIENT_PRELOAD_NONE)
	
	def registerEvent(self, keyName, callback):
		self.client.notify_add(self.keyPath(keyName) , callback)
		
	def getValue(self, type, keyName, default):
		keyPath = self.keyPath(keyName)
		value = self.client.get(keyPath)
		if not value:
			self.setValue(type, keyName, default)
			return default
		return self.getForType(value, type)
		
	def getForType(self, value, type):
		if type not in self.types:
			raise Exception
		get, set = self.types[type]
		return get(value)
	
	def getBooleanPrivate(self, value):
		return value.get_bool()
	
	def getStringPrivate(self, value):
		return value.get_string()
	
	def getIntegerPrivate(self, value):
		return value.get_int()
		
	def getBoolean(self, keyName, default):
		return self.getValue('bool', keyName, default)
		
	def getString(self, keyName, default):
		return self.getValue('string', keyName, default)
		
	def getInteger(self, keyName, default):
		return self.getValue('int', keyName, default)
		
	def getList(self, type, keyName, default):
		keyPath = self.rootFolder + '/' + keyName
		list = self.client.get_list(keyPath, type)
		if len(list) == 0:
			self.client.set_list(keyPath, type, default)
			return default
		return list
	
	def getColor(self, keyName, default):
		if isValidColorValue(default) == False:
				default = "#FFFFFF"
		self.getString(self, keyName, default)
	
	def setValue(self, type, keyName, value):
		get, set = self.types[type]
		set(keyName, value)
	
	def setBoolean(self, keyName, value):
		self.client.set_bool(self.keyPath(keyName), value)
		
	def setString(self, keyName, value):
		self.client.set_string(self.keyPath(keyName), value)
		
	def setInteger(self, keyName, value):
		self.client.set_int(self.keyPath(keyName), value)
	
	def keyPath(self, keyName):
		return os.path.join(self.rootFolder, keyName)

	def setList(self, type, keyName, default):
		keyPath = self.rootFolder + '/' + keyName
		self.client.set_list(keyPath, type, value)

	def allDirs(self, dir):
		return self.client.all_dirs(dir)
	
	def allEntries(self, dir):
		return self.client.all_dirs(dir)
	
	def unset(self, keyName):
		keyPath = self.keyPath(keyName)
		self.client.unset(keyPath)
	
	def removeCurrentDir(self):
		self.client.recursive_unset(self.rootFolder, 10)


if __name__ == "__main__":
	conf = GConfigurator("/apps/test")
	
	print conf.getString("testString", "default")
	conf.setString("testString", "nonDefault")
	print conf.getString("testString", "default")
	
	print conf.getInteger("testInteger", 1)
	conf.setInteger("testInteger", 2)
	print conf.getInteger("testInteger", 1)
	
	print conf.getBoolean("testBoolean", False)
	conf.setBoolean("testBoolean", True)
	print conf.getBoolean("testBoolean", False)
	conf.removeCurrentDir()
